package com.example.administrator.waitan.find;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.example.administrator.waitan.R;
import com.example.administrator.waitan.find.art.ArtActivity;
import com.example.administrator.waitan.find.event.EventActivity;
import com.example.administrator.waitan.find.news.NewsActivity;
import com.example.administrator.waitan.find.life.LifeActivity;
import com.example.administrator.waitan.find.read.ReadActivity;
import com.example.administrator.waitan.hot.HotFragment;
import com.example.administrator.waitan.hot.today.TodayFragment;
import com.example.administrator.waitan.tools.GsonRequest;
import com.example.administrator.waitan.tools.VolleySingleton;

import java.util.ArrayList;

import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2016/1/11.
 */
public class FindFragment extends Fragment implements View.OnClickListener {
    private Context context;
    private RecyclerView todayRecyclerView;
    private LookTodayRecyclerViewAdapter todayAdapter;
    private ArrayList<LookTodayInfo> lookTodayInfos;
    private RequestQueue queue;
    private RecyclerView weekRecyclerView;
    private LookWeekRecyclerViewAdapter lookWeekAdapter;
    private ArrayList<LookWeekInfo> lookWeekInfos;
    private TextView joke;
    private ImageView lookTodayMore, lookWeekMore;
    private FindViewPagerAdapter findViewPagerAdapter;
    private ViewPager findViewPager;
    private String todayUrl = "http://dailyapi.ibaozou.com/api/v1/section/57";
    private String weekUrl = "http://dailyapi.ibaozou.com/api/v1/section/58";
    private String jokeUrl = "http://dailyapi.ibaozou.com/api/v1/section/57";
    private int viewPagerItem = 0;
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            findViewPager.setCurrentItem(viewPagerItem % 3);
            viewPagerItem++;
            return false;
        }
    });
//    private Runnable runnable = new Runnable() {
//        @Override
//        public void run() {
//
//            try {
//                while (true){
//                    Thread.sleep(3000);
//                    handler.sendEmptyMessage(100);
//                }
//
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
////            handler.postDelayed(runnable, 3000);
//        }
//    };



    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_find, null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        todayRecyclerView = (RecyclerView) getActivity().findViewById(R.id.rc_find_lookToday);
        weekRecyclerView = (RecyclerView) getActivity().findViewById(R.id.rc_find_lookWeek);
        findViewPager = (ViewPager) getActivity().findViewById(R.id.iv_find_head_vp);
        findViewPagerAdapter = new FindViewPagerAdapter(getActivity());
        findViewPager.setAdapter(findViewPagerAdapter);

       // handler.postDelayed(runnable, 3000);
        joke = (TextView) getActivity().findViewById(R.id.tv_find_sendWord);
        getActivity().findViewById(R.id.tv_find_lookWeek_more).setOnClickListener(this);
        getActivity().findViewById(R.id.tv_find_lookToday_more).setOnClickListener(this);
        getActivity().findViewById(R.id.btn_find_art).setOnClickListener(this);
        getActivity().findViewById(R.id.btn_find_event).setOnClickListener(this);
        getActivity().findViewById(R.id.btn_find_food).setOnClickListener(this);
        getActivity().findViewById(R.id.btn_find_life).setOnClickListener(this);
        getActivity().findViewById(R.id.btn_find_read).setOnClickListener(this);


        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        Thread.sleep(4000);
                        handler.sendEmptyMessage(0);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        }).start();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        final FindProgressDialog dialog = new FindProgressDialog(context,"加载数据中",R.anim.activity_in);
        dialog.show();
        queue = Volley.newRequestQueue(getActivity());
        GsonRequest<LookTodayBeans> lookTodayBeansGsonRequest = new GsonRequest<LookTodayBeans>(todayUrl, LookTodayBeans.class, new Response.Listener<LookTodayBeans>() {
            @Override
            public void onResponse(LookTodayBeans response) {
                todayAdapter = new LookTodayRecyclerViewAdapter(getActivity(), response.getArticles());
                todayRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
                todayRecyclerView.setAdapter(todayAdapter);

                dialog.dismiss();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                todayAdapter = new LookTodayRecyclerViewAdapter(getActivity(), null);
                todayRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
                todayRecyclerView.setAdapter(todayAdapter);
                dialog.dismiss();

            }
        });
        GsonRequest<LookWeekBeans> lookWeekBeansGsonRequest = new GsonRequest<LookWeekBeans>(weekUrl, LookWeekBeans.class, new Response.Listener<LookWeekBeans>() {
            @Override
            public void onResponse(LookWeekBeans response) {
                lookWeekAdapter = new LookWeekRecyclerViewAdapter(getActivity(), response.getArticles());
                weekRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
                weekRecyclerView.setAdapter(lookWeekAdapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        GsonRequest<JokeTodayBeans> jokeTodayBeansGsonRequest = new GsonRequest<JokeTodayBeans>(jokeUrl, JokeTodayBeans.class, new Response.Listener<JokeTodayBeans>() {
            @Override
            public void onResponse(JokeTodayBeans response) {
                joke.setText(response.getArticles().get(0).getTitle());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        VolleySingleton.getInstance()._addRequest(lookTodayBeansGsonRequest);
        VolleySingleton.getInstance()._addRequest(lookWeekBeansGsonRequest);
        VolleySingleton.getInstance()._addRequest(jokeTodayBeansGsonRequest);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_find_art:
                Intent goArt = new Intent(getActivity(), ArtActivity.class);
                getActivity().startActivity(goArt);
                break;
            case R.id.btn_find_event:
                Intent goEvent = new Intent(getActivity(), EventActivity.class);
                getActivity().startActivity(goEvent);
                break;
            case R.id.btn_find_life:
                Intent goLife = new Intent(getActivity(), LifeActivity.class);
                getActivity().startActivity(goLife);
                break;
            case R.id.btn_find_food:
                Intent goNews = new Intent(getActivity(), NewsActivity.class);
                getActivity().startActivity(goNews);
                break;
            case R.id.btn_find_read:
                Intent goRead = new Intent(getActivity(), ReadActivity.class);
                getActivity().startActivity(goRead);
                break;
            case R.id.tv_find_lookToday_more:
//                post参数为要传的值
                EventBus.getDefault().post("8");
                break;
            case R.id.tv_find_lookWeek_more:
                //post参数为要传的值
                EventBus.getDefault().post("9");
                break;
        }
    }
}
